package com.approval.approvalapp.message.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.approval.approvalapp.R;
import com.approval.approvalapp.bean.MessageListBean;
import com.approval.approvalapp.message.MessageXqActivity;

import java.util.List;

/**
 * Created by louyulin on 2019/4/26.
 */

public class MessageListAdapter extends RecyclerView.Adapter<MessageListAdapter.Holder> {

    List<MessageListBean> messageListBeanList;
    Context context;

    private String from;

    public void setFrom(String from) {
        this.from = from;
        notifyDataSetChanged();
    }

    public void setMessageListBeanList(List<MessageListBean> messageListBeanList) {
        this.messageListBeanList = messageListBeanList;
        notifyDataSetChanged();
    }

    @Override
    public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
        context = parent.getContext();
        View view = LayoutInflater.from(context).inflate(R.layout.message_list_item, parent, false);
        Holder holder = new Holder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(final Holder holder, final int position) {
        holder.tv1.setText(messageListBeanList.get(position).getTitle());
        holder.tv2.setText(messageListBeanList.get(position).getAuthor());
        holder.tv3.setText(messageListBeanList.get(position).getAdd_time());

        if (0 == messageListBeanList.get(position).getIFJS()) {
            holder.view_yellow.setVisibility(View.VISIBLE);
        } else {
            holder.view_yellow.setVisibility(View.GONE);
        }

        if (0 == messageListBeanList.get(position).getTZTYPE()) {
            //会议
            holder.type_iv.setBackgroundResource(R.drawable.ico_message_meeting);

            if (messageListBeanList.get(position).getIFCH() == 0) {
                //未处理
                holder.ckxq_iv.setVisibility(View.VISIBLE);
                holder.ckxq_tv.setVisibility(View.VISIBLE);
                holder.ll.setVisibility(View.GONE);
            } else if (messageListBeanList.get(position).getIFCH() == 1) {
                    //确认参会
                holder.ckxq_iv.setVisibility(View.GONE);
                holder.ckxq_tv.setVisibility(View.GONE);
                holder.ll.setVisibility(View.VISIBLE);
                holder.state_pas.setVisibility(View.VISIBLE);
                holder.wt.setText("确认参会");
                holder.wt.setTextColor(Color.parseColor("#1465FD"));
            } else {
                holder.ckxq_iv.setVisibility(View.GONE);
                holder.ckxq_tv.setVisibility(View.GONE);
                holder.ll.setVisibility(View.VISIBLE);
                holder.state_pas.setVisibility(View.GONE);
                holder.wt.setText("委托" + messageListBeanList.get(position)
                        .getWTNAME() +"参会");
                holder.wt.setTextColor(Color.parseColor("#DE463A"));
            }

        } else {
            holder.type_iv.setBackgroundResource(R.drawable.ico_message_ordinary);
            holder.ckxq_iv.setVisibility(View.VISIBLE);
            holder.ckxq_tv.setVisibility(View.VISIBLE);
            holder.ll.setVisibility(View.GONE);
        }


        holder.root.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                holder.view_yellow.setVisibility(View.GONE);
                Intent intent = new Intent();
                intent.setClass(context, MessageXqActivity.class);
                MessageListBean messageListBean = messageListBeanList.get(position);
                intent.putExtra("bean", messageListBean);
                if (!TextUtils.isEmpty(from)){
                    intent.putExtra("gone", true);
                }
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        if (messageListBeanList == null) {
            return 0;
        } else {
            return messageListBeanList.size();
        }
    }

    class Holder extends RecyclerView.ViewHolder {
        TextView tv1, tv2, tv3;
        RelativeLayout root;
        View view_yellow;
        ImageView type_iv;
        TextView ckxq_tv;
        ImageView ckxq_iv;
        LinearLayout ll;
        ImageView state_pas;
        TextView wt;

        public Holder(View itemView) {
            super(itemView);
            tv1 = itemView.findViewById(R.id.tv1);
            view_yellow = itemView.findViewById(R.id.view_yellow);
            tv2 = itemView.findViewById(R.id.tv2);
            tv3 = itemView.findViewById(R.id.tv3);
            root = itemView.findViewById(R.id.root);
            type_iv = itemView.findViewById(R.id.type_iv);
            ckxq_tv = itemView.findViewById(R.id.ckxq_tv);
            ckxq_iv = itemView.findViewById(R.id.ckxq_iv);
            ll = itemView.findViewById(R.id.ll);
            state_pas = itemView.findViewById(R.id.state_pas);
            wt = itemView.findViewById(R.id.wt);
        }
    }
}
